package kr.jobtc.phonebook;

import java.util.ArrayList;

import org.w3c.dom.Text;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class ListActivity extends Activity {
	ListView lv;
	String findStr=null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list);
		
		list();	
		
	}
	
	public void list(){
		String from[] ={"name","phone"};
		int to[] = {android.R.id.text1, android.R.id.text2};
		
		MyHelper helper = new MyHelper(this);
		Cursor c = helper.list(findStr);

		SimpleCursorAdapter adapter = new SimpleCursorAdapter(
				this, android.R.layout.simple_list_item_2,
				c, from, to	);
		lv = (ListView)findViewById(R.id.listView1);
		lv.setAdapter(adapter);
		
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int pos, long arg3) {
				TextView t1 = (TextView)view.findViewById(android.R.id.text1);
				TextView t2 = (TextView)view.findViewById(android.R.id.text2);
				
				Intent i = new Intent(getApplicationContext(), SearchActivity.class);
				i.putExtra("name", t1.getText().toString());
				i.putExtra("phone",t2.getText().toString());
				
				startActivityForResult(i, 0);
				
			}
		});
	}
	
	public void search(View v ){
		EditText et = (EditText)findViewById(R.id.editText1);
		findStr = et.getText().toString();
		list();
	}
	
	

	@Override
	protected void onResume() {
		list();
		super.onResume();
	}



	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		new OptionMenu(this, item);
		return super.onOptionsItemSelected(item);
	}

}
